package com.lagou.rocketmq.listener;

import com.alibaba.fastjson.JSON;
import com.lagou.rocketmq.service.OrdersService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "broker-a", consumerGroup = "broker-a-consumer-1")
public class PlaceOrder implements RocketMQListener<String> {

    @Resource
    private OrdersService ordersService;

    @Override
    public void onMessage(String s) {
        System.out.println("下单消息:" + s);
        Map<String, Object> msg = JSON.parseObject(s);
        Integer userId = (Integer) msg.get("userId");
        Integer goodId = (Integer) msg.get("goodId");
        ordersService.placeOrder(userId, goodId);
    }

}
